package com.dianping.cat.home.dependency.graph.transform;
import com.dianping.cat.home.dependency.graph.IVisitor;
import com.dianping.cat.home.dependency.graph.entity.TopologyEdge;
import com.dianping.cat.home.dependency.graph.entity.TopologyGraph;
import com.dianping.cat.home.dependency.graph.entity.TopologyNode;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

public class DefaultNativeParser implements IVisitor {
  private DefaultLinker m_linker;

  public DefaultNativeParser(InputStream in) {
/*  15 */     this.m_linker = new DefaultLinker(true);




    this.m_in = new DataInputStream(in);
  }
  private DataInputStream m_in;

  public static TopologyGraph parse(byte[] data) { return parse(new ByteArrayInputStream(data)); }


  public static TopologyGraph parse(InputStream in) {
/*  28 */     DefaultNativeParser parser = new DefaultNativeParser(in);
    TopologyGraph topologyGraph = new TopologyGraph();

    try {
      topologyGraph.accept(parser);
/*  33 */     } catch (RuntimeException e) {
/*  34 */       if (e.getCause() == null || !(e.getCause() instanceof java.io.EOFException))
      {

/*  37 */         throw e;
      }
    }

/*  41 */     parser.m_linker.finish();
/*  42 */     return topologyGraph;
  }



  public void visitTopologyEdge(TopologyEdge topologyEdge) {
    byte tag;
/*  49 */     while ((tag = readTag()) != -1) {
/*  50 */       visitTopologyEdgeChildren(topologyEdge, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitTopologyEdgeChildren(TopologyEdge topologyEdge, int _field, int _type) {
/*  55 */     switch (_field) {
      case 1:
/*  57 */         topologyEdge.setKey(readString());
        break;
      case 2:
/*  60 */         topologyEdge.setType(readString());
        break;
      case 3:
/*  63 */         topologyEdge.setTarget(readString());
        break;
      case 4:
/*  66 */         topologyEdge.setOpposite(readBoolean());
        break;
      case 5:
/*  69 */         topologyEdge.setWeight(readInt());
        break;
      case 6:
/*  72 */         topologyEdge.setStatus(readInt());
        break;
      case 7:
/*  75 */         topologyEdge.setDes(readString());
        break;
      case 8:
        topologyEdge.setLink(readString());
        break;
      case 9:
/*  81 */         topologyEdge.setSelf(readString());
        break;
      case 10:
/*  84 */         topologyEdge.setDashed(readBoolean());
        break;
    }
  }



  public void visitTopologyGraph(TopologyGraph topologyGraph) {
    byte tag;
/*  93 */     if ((tag = readTag()) != -4) {
/*  94 */       throw new RuntimeException(String.format("Malformed payload, expected: %s, but was: %s!", new Object[] { Integer.valueOf(-4), Byte.valueOf(tag) }));
    }

/*  97 */     while ((tag = readTag()) != -1) {
/*  98 */       visitTopologyGraphChildren(topologyGraph, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitTopologyGraphChildren(TopologyGraph topologyGraph, int _field, int _type) {
/* 103 */     switch (_field) {
      case 1:
/* 105 */         topologyGraph.setId(readString());
        break;
      case 2:
/* 108 */         topologyGraph.setType(readString());
        break;
      case 3:
/* 111 */         topologyGraph.setStatus(readInt());
        break;
      case 4:
/* 114 */         topologyGraph.setDes(readString());
        break;
      case 33:
/* 117 */         if (_type == 1) {
/* 118 */           TopologyNode topologyNode = new TopologyNode();

/* 120 */           visitTopologyNode(topologyNode);
/* 121 */           this.m_linker.onTopologyNode(topologyGraph, topologyNode); break;
/* 122 */         }  if (_type == 2) {
/* 123 */           for (int i = readInt(); i > 0; i--) {
/* 124 */             TopologyNode topologyNode = new TopologyNode();

/* 126 */             visitTopologyNode(topologyNode);
/* 127 */             this.m_linker.onTopologyNode(topologyGraph, topologyNode);
          }
        }
        break;
      case 34:
/* 132 */         if (_type == 1) {
/* 133 */           TopologyEdge topologyEdge = new TopologyEdge();

/* 135 */           visitTopologyEdge(topologyEdge);
/* 136 */           this.m_linker.onTopologyEdge(topologyGraph, topologyEdge); break;
/* 137 */         }  if (_type == 2) {
/* 138 */           for (int i = readInt(); i > 0; i--) {
/* 139 */             TopologyEdge topologyEdge = new TopologyEdge();

/* 141 */             visitTopologyEdge(topologyEdge);
/* 142 */             this.m_linker.onTopologyEdge(topologyGraph, topologyEdge);
          }
        }
        break;
    }
  }



  public void visitTopologyNode(TopologyNode topologyNode) {
    byte tag;
/* 153 */     while ((tag = readTag()) != -1) {
/* 154 */       visitTopologyNodeChildren(topologyNode, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitTopologyNodeChildren(TopologyNode topologyNode, int _field, int _type) {
/* 159 */     switch (_field) {
      case 1:
/* 161 */         topologyNode.setId(readString());
        break;
      case 2:
/* 164 */         topologyNode.setType(readString());
        break;
      case 3:
/* 167 */         topologyNode.setStatus(readInt());
        break;
      case 4:
/* 170 */         topologyNode.setDes(readString());
        break;
      case 5:
/* 173 */         topologyNode.setLink(readString());
        break;
      case 6:
/* 176 */         topologyNode.setWeight(readInt());
        break;
    }
  }

  private boolean readBoolean() {
    try {
/* 183 */       return ((this.m_in.readByte() == 1) ? Boolean.TRUE : Boolean.FALSE).booleanValue();
/* 184 */     } catch (IOException e) {
/* 185 */       throw new RuntimeException(e);
    }
  }

  private int readInt() {
    try {
/* 191 */       return (int)readVarint(32);
/* 192 */     } catch (IOException e) {
/* 193 */       throw new RuntimeException(e);
    }
  }

  private String readString() {
    try {
/* 199 */       return this.m_in.readUTF();
/* 200 */     } catch (IOException e) {
/* 201 */       throw new RuntimeException(e);
    }
  }

  private byte readTag() {
    try {
/* 207 */       return this.m_in.readByte();
/* 208 */     } catch (IOException e) {
/* 209 */       throw new RuntimeException(e);
    }
  }

  protected long readVarint(int length) throws IOException {
/* 214 */     int shift = 0;
/* 215 */     long result = 0L;

/* 217 */     while (shift < length) {
/* 218 */       byte b = this.m_in.readByte();
/* 219 */       result |= (b & 0x7F) << shift;
/* 220 */       if ((b & 0x80) == 0) {
/* 221 */         return result;
      }
/* 223 */       shift += 7;
    }

/* 226 */     throw new RuntimeException("Malformed variable int " + length + "!");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/dependency/graph/transform/DefaultNativeParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */